﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;

public partial class SiteUser_CarInsurance_ViewCICustomerCallNewSearch : System.Web.UI.Page
{
    int _currentRow = 0;
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

       

        if (!IsPostBack)
        {
            ResetAll();
            txtAgentId.Text = Session[General.UserId].ToString();
            

            divCallStatus.Visible = false;
            divCallHistory.Visible = false;
            
            if (Request.Params["leadid"] != null)
            {
                lblAgentLeadID.Text = Request.Params["leadid"].ToString();
            }

            if (Request.Params["calltype"] != null)
            {
                lblCallType.Text = Request.Params["calltype"].ToString();

                btnClaim.Visible = false;
                btnNewLead.Visible = false;
                if (Request.Params["calltype"] == "102" || Request.Params["calltype"] == "103")
                {
                    pnlSearch.Visible = false;
                    pnlSerachMobile.Visible = true;
                    if (Request.Params["callmeid"] != null && Request.Params["callmeid"].ToString() != "0")
                    {
                        txtCallMeId.Text = Request.Params["callmeid"].ToString();
                        if (txtCallMeId.Text != string.Empty && txtCallMeId.Text != "0")
                        {
                            LoadCallMeUser(Convert.ToInt64(txtCallMeId.Text));
                            btnSerach_Click(sender, e);
                        }
                    }
                }
                else
                {
                    pnlSearch.Visible = true;
                    pnlSerachMobile.Visible = false;
                }
            }
            else
            {
                pnlSearch.Visible = true;
                pnlSerachMobile.Visible = false;
            }

            if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "100"
            && Session[CE.Global.General.UserId].ToString() != "1499") //Broker 2 is Supervisor
            {
                ddlUserTypeID.Visible = false;
            }
        }
    }

    private void LoadAgentList()
    {
        try
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblList");
            string _spName = "sprocCXCRMGetAgentList";

            _dt = _db.GetDataTable(_spName);
            ddlCBAgentList.Items.Clear();
            if (_dt != null && _dt.Rows.Count > 0)
            {
                ddlCBAgentList.DataSource = _dt;
                ddlCBAgentList.DataTextField = "FULLNAME";
                ddlCBAgentList.DataValueField = "CEID";
                ddlCBAgentList.DataBind();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            throw;
        }
    }

   

    protected void ddlSearchBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlSearchBy.Text == "MOBILE")
        {
            txtMobileNoSearch.MaxLength = 10;
            if (txtMobileNoSearch.Text.Length > 10)
            {
                txtMobileNoSearch.Text = txtMobileNoSearch.Text.Trim().Substring(0, 10);
            }
        }
        else
        {
            txtMobileNoSearch.MaxLength = 50;
        }

       
    }

    protected void ShowCarAndCustomerDetails(Int64 CustomerID, Int64 CarID, Int64 CarPolicyID)
    {
        if (CustomerID > 0 && CarID > 0 && CarPolicyID > 0)
        {
            txtCustomerId.Text = CustomerID.ToString();
            txtCarId.Text = CarID.ToString();
            txtCarPolicyId.Text = CarPolicyID.ToString();
          
            txtCallStatusId.Text = "0";
            ddlCICallStatus.Text = "-1";
            txtCICallMeRemark.Visible = false;
            lblRefCarID.Text = CarID.ToString();

            divCompletedQuoteDetails.Visible = false;
            divCallHistory.Visible = true;

            // From GG Mobile
            MyDatabase _myDb = new MyDatabase();
            object IsMobile = _myDb.ExecuteScalarSqlStatement(@"SELECT p.Info8 FROM tblCarPolicy p
                INNER JOIN tblMobileCarInquiryDetails m ON p.cecarid = m.carid
                WHERE p.CECARID = " + CarID);
            bool _isMobile = false;
            if (IsMobile != null)
            {
                if (IsMobile.ToString().ToUpper() == "MOBILE")
                {
                    _isMobile = true;
                    SortedList _sqlParam = new SortedList();
                    _sqlParam.Add("@CarID", txtCarId.Text);
                    DataTable _dtMobileCompltedQuote = _myDb.GetDataTableBySP("sprocMobileSelectedCompletedUserSelectItem", _sqlParam);

                    if (_dtMobileCompltedQuote != null && _dtMobileCompltedQuote.Rows.Count > 0)
                    {
                        divCompletedQuoteDetails.Visible = true;
                        hplNewQuote.Visible = false;
                        hplReRunQuote.Visible = false;
                        divCustomerDetail.Visible = false;
                        divPolicyDetails.Visible = false;
                        divLinks.Visible = false;
                        gvCarResult.DataSource = null;
                        gvCarResult.DataBind();
                        divResult.Visible = false;
                        divCallHistory.Visible = true;
                        btnNewCall.Visible = true;

                        lblApplicationFormSummaryCaption.Text += lblApplicationFormSummaryCaption.Text + " (Mobile)";

                        lblCQFullName.Text = _dtMobileCompltedQuote.Rows[0]["Name"].ToString();
                        lblCQPaymentType.Text = _dtMobileCompltedQuote.Rows[0]["Payment"].ToString();

                        lblCQOrderRefNo.Text = "";
                        lblCQPackageName.Text = _dtMobileCompltedQuote.Rows[0]["CoverTypeDesc"].ToString();
                        lblCQPremiumAmount.Text = _dtMobileCompltedQuote.Rows[0]["PAnnualPremium"].ToString();
                        lblCQCTPLStatus.Text = "";

                        lblCQDateTime.Text = Convert.ToDateTime(_dtMobileCompltedQuote.Rows[0]["ModifiedDate"].ToString()).ToString("dd-MMM-yyyy hh:mm:ss tt");


                        btnNext.Visible = true;
                        btnCallStatus.Enabled = false;
                        divCompletedQuoteDetails.Visible = true;
                    }
                }
            }

            // From GG Web
            if (!_isMobile)
            {
                Int64 _providerCompanyId = 0;
                object _objPID = _myDb.ExecuteScalarSqlStatement("select ProviderCompanyID from tblCarInsuranceSelectItemTH Where CarID = " + txtCarId.Text + " And RefCEUserID = " + txtCustomerId.Text);

                if (_objPID != null)
                {
                    _providerCompanyId = Convert.ToInt64(_objPID);

                    SortedList _sqlParam = new SortedList();
                    _sqlParam.Add("@CarID", txtCarId.Text);
                    _sqlParam.Add("@ProviderCompanyID", _providerCompanyId.ToString());
                    DataTable _dtCompltedQuote = _myDb.GetDataTableBySP("sprocCarInsuranceTHCompletedUserSelectItem", _sqlParam);

                    if (_dtCompltedQuote != null && _dtCompltedQuote.Rows.Count > 0)
                    {
                        divCompletedQuoteDetails.Visible = true;
                        hplNewQuote.Visible = false;
                        hplReRunQuote.Visible = false;
                        divCustomerDetail.Visible = false;
                        divPolicyDetails.Visible = false;
                        divLinks.Visible = false;
                        gvCarResult.DataSource = null;
                        gvCarResult.DataBind();
                        divResult.Visible = false;
                        divCallHistory.Visible = true;
                        btnNewCall.Visible = true;

                        lblCQFullName.Text = _dtCompltedQuote.Rows[0]["Name"].ToString();
                        lblCQPaymentType.Text = _dtCompltedQuote.Rows[0]["Payment"].ToString();

                        lblCQOrderRefNo.Text = _dtCompltedQuote.Rows[0]["OrderRefNo"].ToString();
                        lblCQPackageName.Text = _dtCompltedQuote.Rows[0]["CoverTypeDesc"].ToString();
                        lblCQPremiumAmount.Text = _dtCompltedQuote.Rows[0]["PAnnualPremium"].ToString();
                        lblCQCTPLStatus.Text = _dtCompltedQuote.Rows[0]["IsCTPL"].ToString();

                        lblCQDateTime.Text = Convert.ToDateTime(_dtCompltedQuote.Rows[0]["ModifiedDate"].ToString()).ToString("dd-MMM-yyyy hh:mm:ss tt");


                        btnNext.Visible = true;
                        btnCallStatus.Enabled = false;
                        divCompletedQuoteDetails.Visible = true;
                    }
                }
            }
            
            if (divCompletedQuoteDetails.Visible == false)
            {
                hplNewQuote.Visible = true;
                hplReRunQuote.Visible = true;
                divResult.Visible = true;
                
                ResultsQuotesDataBind();


              
                gvCarResult.Sort("AnnualPremium", SortDirection.Ascending);
                gvCarResult.SelectedIndex = 0;
                txtCarResultId.Text = "0";
                txtCXCRMId.Text = "0";
                txtCICallMeRemark.Text = "";
                lblCallStatusMessage.Text = "";
                ddlCICallStatus.Text = "-1";
                txtCallStatusId.Text = "0";

                if (txtCustomerId.Text != string.Empty && txtCarId.Text != string.Empty)
                {
                    btnNewCall.Visible = true;
                    MyDatabase _db = new MyDatabase();
                    //Customer Details
                    string _strSelect = "select P.FirstName, P.SurName, P.DOB, P.Gender, C.Email, C.Mobile, O.TH Occupation from tblCEUsrProfile P "
                                        + "INNER JOIN tblOccupationInfo O ON P.Occupation = O.OccupationId "
                                        + "INNER JOIN tblCEContact C ON C.RefCEUserID = P.RefCEUserID "
                                        + "Where P.RefCEUserId = " + txtCustomerId.Text;
                    DataTable _dtSelect = new DataTable("dtSelect");
                    _dtSelect = _db.GetDataTable(_strSelect);
                    if (_dtSelect != null && _dtSelect.Rows.Count > 0)
                    {
                        lblName.Text = _dtSelect.Rows[0]["FirstName"].ToString();
                        lblLastName.Text = _dtSelect.Rows[0]["SurName"].ToString();
                        lblGender.Text = _dtSelect.Rows[0]["Gender"].ToString() == "M" ? "ชาย" : "หญิง";
                        lblDOB.Text = Convert.ToDateTime(_dtSelect.Rows[0]["DOB"].ToString()).ToString("dd/MMM/yyyy");
                        lblOccupation.Text = _dtSelect.Rows[0]["Occupation"].ToString();
                        lblMobileNo.Text = _dtSelect.Rows[0]["Mobile"].ToString();
                        lblRefMobile.Text = lblMobileNo.Text;
                        lblEmailAddress.Text = _dtSelect.Rows[0]["Email"].ToString();
                        lblRefEmail.Text = lblEmailAddress.Text;
                    }
                    _dtSelect = null;
                    _dtSelect = new DataTable("dtSelect");

                    _strSelect = " Select CD.MakeModelDesc, CD.ManufactYear, P.TH CurrentInsurer "
                                + " from tblCarDetailTH CD"
                                + " INNER JOIN tblPreInsurer P ON CAST(CD.Info1 AS INT) = P.PreInsurerId"
                                + " Where P.CompanyType = 1 And P.ServiceType = 100 AND CD.RefCEUserId = " + txtCustomerId.Text + " And CD.CarId= " + txtCarId.Text;
                    _dtSelect = _db.GetDataTable(_strSelect);
                    if (_dtSelect != null && _dtSelect.Rows.Count > 0)
                    {
                        lblCarMakeModel.Text = _dtSelect.Rows[0]["MakeModelDesc"].ToString();
                        lblRegYear.Text = _dtSelect.Rows[0]["ManufactYear"].ToString();
                        lblCurrentInsurer.Text = _dtSelect.Rows[0]["CurrentInsurer"].ToString();

                    }

                    _dtSelect = null;
                    _dtSelect = new DataTable("dtSelect");
                    bool _IsClaim = false;
                    bool _IsAddDriver = false;
                    string _vehicleType;
                    DateTime _dtCoverCommDate = dateTimeNow;
                    _strSelect = "Select InsuranceType, GarageType, VehicleType, SumInsured, NCDPercent, IsClaimLastYear, IsCTPL , IsAddDriver, NoOfDriver, CoverageCommenceDate, ModifiedDate "
                        + " from tblCarPolicy"
                        + " Where RefCEUserId = " + txtCustomerId.Text + " And CECarId= " + txtCarId.Text;
                    _dtSelect = _db.GetDataTable(_strSelect);
                    if (_dtSelect != null && _dtSelect.Rows.Count > 0)
                    {
                        lblInsuranceType.Text = _dtSelect.Rows[0]["InsuranceType"].ToString().Substring(0, 1);
                        lblGarageType.Text = _dtSelect.Rows[0]["GarageType"].ToString() == "1" ? "อู่ประกัน" : "อู่ห้าง";
                        _vehicleType = _dtSelect.Rows[0]["VehicleType"].ToString();
                        switch (_vehicleType)
                        {
                            case "110":
                                lblVehicleType.Text = "รถยนต์ส่วนบุคคล";
                                break;
                            case "120":
                                lblVehicleType.Text = "รถสาธารณะ";
                                break;
                            default:
                                lblVehicleType.Text = _vehicleType;
                                break;
                        }

                        lblRefModifiedDateTime.Text = Convert.ToDateTime(_dtSelect.Rows[0]["ModifiedDate"].ToString()).ToString("dd/MMM/yyyy hh:mm tt");
                        lblSumInsured.Text = Convert.ToInt32(_dtSelect.Rows[0]["SumInsured"]).ToString("#,000");
                        lblNCD.Text = _dtSelect.Rows[0]["NCDPercent"].ToString() == "-1" ? "0" : _dtSelect.Rows[0]["NCDPercent"].ToString() + "%";

                        lblCTPL.Text = Convert.ToBoolean(_dtSelect.Rows[0]["IsCTPL"]) == true ? "ใช่" : "ไม่ต้องการ";
                        _IsClaim = Convert.ToBoolean(_dtSelect.Rows[0]["IsClaimLastYear"].ToString());
                        _IsAddDriver = Convert.ToBoolean(_dtSelect.Rows[0]["IsAddDriver"].ToString());
                        string _coverCommDate = _dtSelect.Rows[0]["CoverageCommenceDate"].ToString();

                        if (DateTime.TryParse(_coverCommDate, out _dtCoverCommDate))
                        {
                            lblCoverCommencementDate.Text = _dtCoverCommDate.ToString("dd/MMM/yyyy");
                        }

                        if (_IsAddDriver == true)
                        {
                            lblDriverCount.Text = _dtSelect.Rows[0]["NoOfDriver"].ToString();
                        }
                        else
                        {
                            lblDriverCount.Text = "0";
                        }
                        lblClaimInfo.Text = _IsClaim == true ? "ใช่" : "ไม่มี";

                    }

                    _dtSelect = null;
                    _dtSelect = new DataTable("dtSelect");

                    _db = null;
                    _dtSelect = null;

                    grvPremiumList.DataBind();
                    divCustomerDetail.Visible = true;
                    divPolicyDetails.Visible = true;
                    divPremiumList.Visible = true;
                    CheckApplicationStatus();
                    SetGVPremiumSelectedIndex();
                }
            }

           
        }
    }

    protected void gvCarResult_Sorting(object sender, GridViewSortEventArgs e)
    {
    }

    protected void gvCustomerList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (gvCustomerList.Rows.Count > 0 && gvCustomerList.SelectedIndex >= 0)
        {
            DataKey myData = gvCustomerList.SelectedDataKey;
            txtOldCustomerId.Text = myData[0].ToString();
            txtCustomerId.Text = myData[0].ToString();
            gvCarDetailList.DataBind();
            if (gvCarDetailList.Rows.Count > 0)
            {
                divCarDetailList.Visible = true;
                gvCarDetailList.SelectedIndex = 0;
                gvCarDetailList_SelectedIndexChanged(sender, e);

                divLinks.Visible = false;
                lblNewCallMessage.Text = string.Empty;
                if (Request.Params["page"] != null && Request.Params["page"].ToString().ToUpper() == "OLDQUOTE")
                {
                    hplOldQuote.Visible = true;
                    btnRenewQuote.Visible = false;
                }
                else
                {
                    hplOldQuote.Visible = false;
                }
            }
            else
            {
                divCarDetailList.Visible = true;
                divCompletedQuoteDetails.Visible = false;
                divCustomerDetail.Visible = false;
                divPolicyDetails.Visible = false;
                divCallHistory.Visible = false;
                divLinks.Visible = false;
                divResult.Visible = false;
                divCallStatus.Visible = false;
                hplOldQuote.Visible = false;

            }
        }
    }

    protected void ResetAll()
    {
        txtCarId.Text = "";
        txtCustomerId.Text = "";
        txtCarPolicyId.Text = "";
        hplReRunQuote.NavigateUrl = "";
        hplNewQuote.NavigateUrl = "";

        lblCallStatusMessage.Text = "";
        divLinks.Visible = false;
        divResult.Visible = false;
        divCallStatus.Visible = false;
        divCustomerDetail.Visible = false;
        divPolicyDetails.Visible = false;
        divCompletedQuoteDetails.Visible = false;
        divCallHistory.Visible = false;
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewCICustomerCallNew.aspx");
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewCICusotmerCallNew.aspx?calltype=" + lblCallType.Text);
    }

    protected void btnCallStatus_Click(object sender, EventArgs e)
    {
        
    }

    protected void btnBuyNowRefresh_Click(object sender, EventArgs e)
    {
        
        gvCarDetailList_SelectedIndexChanged(sender, e);
        if (divCompletedQuoteDetails.Visible == true)
        {
            lblBuyNowStatus.Text = "<b style='color:red;'>" + Resources.ResultPage.SmsgCongratulation + "</b><br/>" + Resources.ResultPage.SmsgPleaseCheckCompleted + "<br/> " + Resources.ResultPage.SmsgUpdateCallStatus + "<br/>";
        }
        else
        {
            divBuyNowStatus.Visible = true;
            lblBuyNowStatus.Text = "<b style='color:red;'>" + Resources.ResultPage.SmsgProposalnotCompleted + "</b><br/> " + Resources.ResultPage.SmsgPleaseCheckandTry + "<br/>";
        }

    }

    protected void gvCarResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("BuyNow"))
        {
            int index = Convert.ToInt32(e.CommandArgument);
            DataKey myData = gvCarResult.DataKeys[index];

            Session[CE.Global.General.CarResultId] = myData[0].ToString();
            Session[CE.Global.General.ProviderCompanyId] = myData[1].ToString();
            Int64 _resultId = Convert.ToInt64(myData[0].ToString());
            Int64 _companyId = Convert.ToInt64(myData[1].ToString());
            string _buyurl = myData[2].ToString();
            string proposalURL = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CIServerPath");

            proposalURL = proposalURL + "agentlogin.aspx?" + _buyurl;
            ClientScript.RegisterClientScriptBlock(this.GetType(), "OpenBuyNow", "var myRef = window.open('" + proposalURL + "','mywin','left=20,top=20,width=800,height=600,resizable=1,scrollbars=1');", true);
            divBuyNowStatus.Visible = true;
            divResult.Visible = false;
            divCallStatus.Visible = false;
            divLinks.Visible = false;

            MyDatabase _db = new MyDatabase();
            DataTable _dt = new DataTable("tblSelectedItem");
            SortedList _paraList = new SortedList();
            _paraList.Add("@CarResultID", myData[0].ToString());
            _dt = _db.GetDataTableBySP("sproceCXCRMGetSelectedResultItem", _paraList);
            lblBuyNowStatus.Visible = true;
            btnBuyNowRefresh.Visible = true;
            lblBuyNowStatus.Text = "<b style='color:red;'>" + Resources.ResultPage.SmsgBuyOnline + "</b> " + Resources.ResultPage.SmsgButtonClicked + " <br/>" + Resources.ResultPage.SmsgCheckandClick + " <b style='color:green;'>\"Refresh\"</b> " + Resources.ResultPage.SmsgButtontoCheckAppFormStatus + "";
            if (_dt != null && _dt.Rows.Count > 0)
            {
                System.Text.StringBuilder _strBuilder = new System.Text.StringBuilder();
                _strBuilder.Append("<br/><br/><table width='50%' cellspacing='5' cellpadding='5'>");
                _strBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgCompany + "</td><td>:</td><td><b>" + _dt.Rows[0]["CompanyName"].ToString() + "</b></td></tr>");
                _strBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgPremium + "</td><td>:</td><td><b>" + Convert.ToDouble(_dt.Rows[0]["AnnualPremium"].ToString()).ToString("#,000.00") + "</b></td></tr>");
                _strBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgPackage + "</td><td>:</td><td><b>" + _dt.Rows[0]["CoverTypeDesc"].ToString() + "</b></td></tr>");
                _strBuilder.Append("</table>");
                lblBuyNowStatus.Text += _strBuilder.ToString();
            }
        }
    }

    protected void gvCarResult_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = cell.Controls[0] as LinkButton;
                    if (button != null)
                    {
                        Image imageSort = new Image();
                        imageSort.ImageUrl = "~/Images/icons/clear.gif";
                        HtmlGenericControl gv = new HtmlGenericControl("div");
                        Label lnkName = new Label();
                        lnkName.Text = button.Text;

                        if (ViewState["SortExpression"] != null && ViewState["SortExpression"].ToString() == button.CommandArgument.ToString())
                        {
                            if (ViewState["SortOrder"] != null && ViewState["SortOrder"].ToString() == SortDirection.Descending.ToString())
                            {
                                imageSort.ImageUrl = "~/Images/icons/Sort_Down.gif";
                            }
                            else
                            {
                                imageSort.ImageUrl = "~/Images/icons/Sort_Up.gif";
                            }
                        }
                        gv.Controls.Add(lnkName);
                        gv.Controls.Add(imageSort);
                        button.Controls.Add(gv);
                    }
                }
            }

            //Creat Custome Header
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow =
            new GridViewRow(0, 0, DataControlRowType.Header,
            DataControlRowState.Insert);

            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.HorizontalAlign = HorizontalAlign.Center;
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "<img src='../images/arrows/arrow-down.png\'  />" + Resources.CarInsuranceResultPage.OwnDamage + "<img src=\"../images/arrows/arrow-down.png\" align=\"absmiddle\" alt=\"\" />";
            HeaderCell.Font.Bold = true;
            HeaderCell.ControlStyle.CssClass = "green_top";
            HeaderCell.HorizontalAlign = HorizontalAlign.Center;
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "<img src=\"../images/arrows/arrow-down.png\" align=\"absmiddle\" alt=\"\" /> " + Resources.CarInsuranceResultPage.ThirdPartyLiability + "<img src=\"../images/arrows/arrow-down.png\" align=\"absmiddle\" alt=\"\" /><br/>" + Resources.CarInsuranceResultPage.PerAccident;
            HeaderCell.Font.Bold = true;
            HeaderCell.ControlStyle.CssClass = "blue_top";
            HeaderCell.HorizontalAlign = HorizontalAlign.Center;
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "<img src=\"../images/arrows/arrow-down.png\" align=\"absmiddle\" alt=\"\" />" + Resources.CarInsuranceResultPage.AdditionalCovers + "<img src=\"../images/arrows/arrow-down.png\" align=\"absmiddle\" alt=\"\" />";
            HeaderCell.Font.Bold = true;
            HeaderCell.ControlStyle.CssClass = "gold_top";
            HeaderCell.HorizontalAlign = HorizontalAlign.Center;
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "";
            HeaderCell.HorizontalAlign = HorizontalAlign.Center;
            //HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);

            gvCarResult.Controls[0].Controls.AddAt
            (0, HeaderGridRow);
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibtBuyNow = (ImageButton)e.Row.FindControl("ibtBuyNow");

            ibtBuyNow.CommandArgument = e.Row.RowIndex.ToString();

            HyperLink _lnkTest = new HyperLink();
            ibtBuyNow.Attributes.Add("onmouseover", "this.src='" + _lnkTest.ResolveClientUrl(Resources.ImageResource.BuyOnlineButtonHoverImageUrl) + "';");
            ibtBuyNow.Attributes.Add("onmouseout", "this.src='" + _lnkTest.ResolveClientUrl(Resources.ImageResource.BuyOnlineButtonImageUrl) + "';");
            _lnkTest = null;

        }
    }

    protected void gvCarResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //For Thailand
        if (e.Row.RowType == DataControlRowType.DataRow)
        {


            string _callCarProviderId = "";

            string _companyId = DataBinder.Eval(e.Row.DataItem, "ProviderCompanyId").ToString();
            ImageButton btnApplyNow = (ImageButton)e.Row.FindControl("ibtBuyNow");
            btnApplyNow.CommandArgument = e.Row.RowIndex.ToString();

            _callCarProviderId = _companyId;

            if (e.Row.DataItem != null)
            {
                _currentRow += 1;
                
            }

            HtmlGenericControl divFrame = new HtmlGenericControl("div");
            divFrame.Attributes.Add("id", "framework_moreinfo");

            HtmlGenericControl divContainer = new HtmlGenericControl("div");
            divContainer.Attributes.Add("id", "container_moreinfo");

            HtmlGenericControl divContent = new HtmlGenericControl("div");
            divContent.Attributes.Add("id", "content_moreinfo");

            Label lblPolicyInfoId = new Label();
            lblPolicyInfoId = (Label)e.Row.FindControl("lblPolicyInfoId");
            object _objPolicyinfoId = 4;//DataBinder.Eval(e.Row.DataItem, "NInfo3");

            Label lblPromoId = new Label();
            lblPromoId = (Label)e.Row.FindControl("lblPromoId");
            object _objPromoId = 11;//DataBinder.Eval(e.Row.DataItem, "NInfo2");

            Label lblPolicyId = new Label();
            lblPolicyId = (Label)e.Row.FindControl("lblPolicyId");
            object _objPolicyId = 5;//DataBinder.Eval(e.Row.DataItem, "NInfo1");

            

            //Company Logo and Cover Type
            Label lblCoverTypeDesc = (Label)e.Row.FindControl("lblPackageName");
            Image imgCompanyLogo = (Image)e.Row.FindControl("imgCompanyLogo");




            string _logoUrl = DataBinder.Eval(e.Row.DataItem, "CompanyLogoServerUrl").ToString();
            string _coverTypeDesc = DataBinder.Eval(e.Row.DataItem, "PackageName").ToString().Trim();

            imgCompanyLogo.ImageUrl = _logoUrl;
            imgCompanyLogo.ToolTip = _coverTypeDesc;
            lblCoverTypeDesc.Text = _coverTypeDesc;
           

            ////CTPL
            Label lblCTPL = (Label)e.Row.FindControl("lblCTPL");
            Label lblCTPLSymbol = (Label)e.Row.FindControl("lblCTPLSymbol");
            Image imgCTPL = (Image)e.Row.FindControl("imgCTPL");
            string _totalCMI = DataBinder.Eval(e.Row.DataItem, "TotalCMI").ToString().Trim();

            if (_totalCMI != null && _totalCMI != string.Empty && Convert.ToSingle(_totalCMI) > 0.0f)
            {
                imgCTPL.Visible = false;
                lblCTPL.Text = _totalCMI;
                lblCTPL.Visible = true;
                lblCTPLSymbol.Visible = true;
            }
            else
            {
                imgCTPL.Visible = true;
                lblCTPL.Text = "0";
                lblCTPL.Visible = false;
                lblCTPLSymbol.Visible = false;
            }
            ////END CTPL

            //// DEC
            Label lblDEC = (Label)e.Row.FindControl("lblDEC");
            Label lblDECSymbol = (Label)e.Row.FindControl("lblDECSymbol");
            Label lblDECZero = (Label)e.Row.FindControl("lblDECZero");
            string _DEC = DataBinder.Eval(e.Row.DataItem, "DEC").ToString().Trim();

            if (_DEC != null && _DEC != string.Empty && Convert.ToSingle(_DEC) > 0.0f)
            {
                lblDECZero.Visible = false;
                lblDEC.Text = _DEC;
                lblDEC.Visible = true;
                lblDECSymbol.Visible = true;
            }
            else
            {
                lblDECZero.Visible = true;
                lblDEC.Text = "0";
                lblDEC.Visible = false;
                lblDECSymbol.Visible = false;
            }
            //// END DEC

            ///SUM INSURED OR OWN DAMAGE FIRE OR THIEF
            Label lblSumInsured = (Label)e.Row.FindControl("lblSumInsured");
            Label lblSumInsuredSymbol = (Label)e.Row.FindControl("lblSumInsuredSymbol");
            Image imgSumInsured = (Image)e.Row.FindControl("imgSumInsured");
            string _sumInsured = DataBinder.Eval(e.Row.DataItem, "SumInsured").ToString().Trim();

            if (_sumInsured != null && _sumInsured != string.Empty && Convert.ToSingle(_sumInsured) > 0.0f)
            {
                imgSumInsured.Visible = false;
                lblSumInsured.Text = Convert.ToInt32(_sumInsured).ToString("#,000"); //_sumInsured;
                lblSumInsured.Visible = true;
                lblSumInsuredSymbol.Visible = true;
            }
            else
            {
                imgSumInsured.Visible = true;
                lblSumInsured.Text = "0";
                lblSumInsured.Visible = false;
                lblSumInsuredSymbol.Visible = false;
            }
            ///END SUM INSURED OR OWN DAMAGE FIRE OR THIEF

            //GARAGE

            //HyperLink hplnkWorkshop = new HyperLink();
            HyperLink hplnkWorkshop = (HyperLink)e.Row.FindControl("hplWorkshopLink");
           
            hplnkWorkshop.Target = "_blank";
            hplnkWorkshop.Font.Bold = true;
            if (_companyId == "11")
            {
                // "Allianz C.P";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AllianceWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AllianceWorkshopEN");
                }

            }
            else if (_companyId == "12")
            {
                //lblPromo.Text = "MSIG";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MSIGWorkshopEN");
                }
            }
            else if (_companyId == "16")
            {
                //lblPromo.Text = "AXA";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AXAWorkshopEN");
                }
            }
            else if (_companyId == "29")
            {
                //lblPromo.Text = "LMG Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.LMGWorkshopEN");
                }
            }
            else if (_companyId == "31")
            {
                //lblPromo.Text = "Chartis";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ChartisWorkshopEN");
                }
            }
            else if (_companyId == "32")
            {
                //lblPromo.Text = "Bangkok Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.BKIWorkshopEN");
                }
            }
            else if (_companyId == "33")
            {
                //lblPromo.Text = "MTI Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MTIWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MTIWorkshopTH");
                }
            }
            else if (_companyId == "35")
            {
                //lblPromo.Text = "KPI Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KPIWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KPIWorkshopTH");
                }
            }
            else if (_companyId == "39")
            {
                //lblPromo.Text = "KPI Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MittareWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.MittareWorkshopEN");
                }
            }
            else if (_companyId == "41")
            {
                //lblPromo.Text = "Kurnia Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KurniaWorkShopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KurniaWorkShopTH");
                }
            }
            else if (_companyId == "43")
            {
                //lblPromo.Text = "Kurnia Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KurniaWorkShopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.KurniaWorkShopTH");
                }
            }
            else if (_companyId == "43")
            {
                //lblPromo.Text = "TMS Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.SMKWorkShopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.SMKWorkShopEN");
                }
            }
            else if (_companyId == "45")
            {
                //lblPromo.Text = "Viriyah Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ViriyahWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ViriyahWorkshopEN");
                }
            }
            else if (_companyId == "46")
            {
                //lblPromo.Text = "TMS Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.TokioWorkShopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.TokioWorkShopEN");
                }
            }
            else if (_companyId == "58")
            {
                //lblPromo.Text = "TMS Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ThaivivatWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ThaivivatWorkshopEN");
                }
            }
            else if (_companyId == "66")
            {
                //lblPromo.Text = "Dhipaya Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.DhipayaWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.DhipayaWorkshopEN");
                }
            }
            else if (_companyId == "67")
            {
                //lblPromo.Text = "Deves Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.DevesWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.DevesWorkshopEN");
                }
            }
            else if (_companyId == "68")
            {
                //lblPromo.Text = "Thaisri Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ThaiSriWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.ThaiSriWorkshopEN");
                }
            }
            else if (_companyId == "69")
            {
                //lblPromo.Text = "Thaisri Insurance";
                if (HttpContext.Current.Session["Language"].ToString() == "th-TH")
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AssetsWorkshopTH");
                }
                else
                {
                    hplnkWorkshop.NavigateUrl = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.AssetsWorkshopEN");
                }
            }
            //END GARAGE

        }
    }

    private void ResultsQuotesDataBind()
    {
        GetTempResultTableTwo();
        DataView dv = new DataView();
        if (Session[General.UserId] != null)
        {
            dv = new DataView((DataTable)Cache["CarResult" + Session[General.UserId].ToString() + txtCarId.Text]);
        }
        else
        {
            dv = new DataView((DataTable)Cache["CarResult" + txtCarId.Text]);
        }

        gvCarResult.DataSource = dv;
        gvCarResult.DataBind();
    }

    private void GetTempResultTableTwo()
    {
        MyDatabase _db = new MyDatabase();
        DataTable _dtResult = new DataTable();
        string _spName = "[sprocCarInsuranceResultCallMeSelectItemList]";
        SortedList _sPara = new SortedList();
        //test
        _sPara.Add("@CarId", txtCarId.Text);

        if (Session[General.UserId] != null)
        {
            _sPara.Add("@RefUserId", Session[General.UserId].ToString());
        }
        else
        {
            _sPara.Add("@RefUserId", 0);
        }
        if (Convert.ToInt64(txtAgentId.Text) > 0)
        {
            _sPara.Add("@AgentId", Convert.ToInt64(txtAgentId.Text));
        }
        else
        {
            _sPara.Add("@AgentId", 0);
        }
        _dtResult = _db.GetDataTableBySP(_spName, _sPara);
        

        if (Session[General.UserId] != null)
        {
            Cache["CarResult" + Session[General.UserId].ToString() + txtCarId.Text] = _dtResult;
        }
        else
        {
            Cache["CarResult" + txtCarId.Text] = _dtResult;
        }

    }

    protected void btnNewCall_Click(object sender, EventArgs e)
    {
        try
        {
            //if (lblCallType.Text != string.Empty && (lblCallType.Text == "102" || lblCallType.Text == "103"))
            //{
            //    Int64 _agentLeadId = 0;
            //    if (!Int64.TryParse(lblAgentLeadID.Text, out _agentLeadId))
            //        _agentLeadId = 0;

            //    bool isExist = false;
            //    if (_agentLeadId == 0)
            //    {
            //        isExist = GetExistingLead();
            //    }

            //    if (!isExist) btnNewCallMeCall_Click(sender, e);
            //}
            //else
            //{
            //    GetExistingLead();
            //}

            Int64 _agentLeadId = 0;
            if (!Int64.TryParse(lblAgentLeadID.Text, out _agentLeadId))
                _agentLeadId = 0;

            bool isExist = false;
            if (_agentLeadId == 0)
            {
                isExist = GetExistingLead();
            }

            if (!isExist) btnNewCallMeCall_Click(sender, e);
        }
        catch (Exception Ex)
        {
            lblNewCallMessage.Text = Ex.ToString();
        }
    }

    private bool GetExistingLead()
    {
        try
        {
            if (txtCustomerId.Text != string.Empty && txtCarId.Text != string.Empty && txtAgentId.Text != string.Empty)
            {
                MyDatabase _db = new MyDatabase();
                DataTable _dt = new DataTable("tblNewCall");
                string _strReturnMessage = string.Empty;

                string _spName = "sprocCXCRMCheckNewRegularCall";
                SortedList _paraList = new SortedList();
                _paraList.Add("@CustomerID", txtCustomerId.Text);
                _paraList.Add("@CarID", txtCarId.Text);
                _paraList.Add("@AgentID", txtAgentId.Text);
                if (lblCallType.Text == "102" && lblAgentLeadID.Text != string.Empty && lblAgentLeadID.Text != "0")
                {
                    _paraList.Add("@pAgentLeadID", lblAgentLeadID.Text);
                }
                _paraList.Add("@pCallType", lblCallType.Text);

                _dt = _db.GetDataTableBySP(_spName, _paraList);

                Int16 _callStatus = 0;
                Int16 _callType = 0;
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    if (_dt.Rows[0].Table.Columns.Contains("OtherAgentID"))
                    {
                        System.Text.StringBuilder _strReturnMessageBuilder = new System.Text.StringBuilder();
                        _strReturnMessageBuilder.Append("<table bgcolor='#ffffcc' width='450' cellpadding='5' cellspacing='5'>");
                        _strReturnMessageBuilder.Append("<tr><td colspan='3'><b style='color:red;'>" + Resources.General.FOUND + ":" + Resources.ResultPage.SmsgSorryFoundOtherAgentRecord + "</b></tr>");
                        _strReturnMessage += Resources.ResultPage.SmsgAgent + ": <b>" + _dt.Rows[0]["OtherAgentName"].ToString() + "</b>";
                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Broker + "</td><td>:</td><td><b>" + _dt.Rows[0]["OtherAgentName"].ToString() + "</b></td></tr>");

                        _callType = Convert.ToInt16(_dt.Rows[0]["CallType"].ToString());
                        if (_callType == 100)
                        {
                            //_strReturnMessage += " Type: <b style='color:green;'>REGULAR</b>";
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.CallType + "</td><td>:</td><td><b style='color:green;'>" + Resources.General.FirstFollowUpCall + "</b></td></tr>");
                        }
                        else if (_callType == 101)
                        {
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.CallType + "</td><td>:</td><td><b style='color:red;'>" + Resources.General.CallMeRequest + "</b></td></tr>");
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgTime + "</td><td>:</td><td><b>" + _dt.Rows[0]["CallTime"].ToString() + "</b></td></tr>");
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.CarInsurance.CompanyName + "</td><td>:</td><td><b>" + _dt.Rows[0]["ProviderCompanyName"].ToString() + "</b></td></tr>");
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgRequestOn + "</td><td>:</td><td><b>" + _dt.Rows[0]["CallMeRequestDateTime"].ToString() + "</b></td></tr>");
                        }

                        _callStatus = Convert.ToInt16(_dt.Rows[0]["CallStatus"].ToString());
                        if (_callStatus == 1)
                        {
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Status + "</td><td>:</td><td><b style='color:green;'>" + Resources.General.Pending + "</b></td></tr>");
                        }
                        else if (_callStatus == 2)
                        {
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Status + "</td><td>:</td><td><b style='color:red;'>" + Resources.General.Callback + "</b></td></tr>");
                        }
                        else if (_callStatus == 3)
                        {
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Status + "</td><td>:</td><td><b style='color:blue;'>" + Resources.General.FollowUp + "</b></td></tr>");
                        }

                        if (_dt.Rows[0]["OtherAgentCallBackDateTime"] != DBNull.Value)
                        {
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.Callback + " " + Resources.General.DateTime + "</td><td>:</td><td><b>" + Convert.ToDateTime(_dt.Rows[0]["OtherAgentCallBackDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b></td></tr>");
                        }
                        if (_dt.Rows[0]["ModifiedDateTime"] != DBNull.Value)
                        {
                            _strReturnMessageBuilder.Append("<tr><td>" + Resources.General.DateTime + "</td><td>:</td><td><b>" + Convert.ToDateTime(_dt.Rows[0]["ModifiedDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b></td></tr>");
                        }

                        _strReturnMessageBuilder.Append("<tr><td>" + Resources.ResultPage.SmsgTakeOver + "</td><td>:</td><td><a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallNow + "\");' href='offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["OtherAgentLeadID"].ToString() + "&carid=" + txtCarId.Text + "&custid=" + _dt.Rows[0]["OtherAgentCustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "&otheragentid=" + _dt.Rows[0]["OtherAgentID"].ToString() + "'><b>[" + Resources.ResultPage.SmsgCallNow + "]</b></a></td>");
                        _strReturnMessageBuilder.Append("</table>");

                        lblNewCallMessage.Text = _strReturnMessageBuilder.ToString();
                    }
                    else if (_dt.Rows[0].Table.Columns.Contains("AgentLeadID"))
                    {
                        _callStatus = 1;
                        if (_dt.Rows[0].Table.Columns.Contains("CallStatus"))
                        {
                            if (_dt.Rows[0]["CallStatus"] != DBNull.Value)
                            {
                                _callStatus = Convert.ToInt16(_dt.Rows[0]["CallStatus"].ToString());
                            }
                        }

                        _callType = Convert.ToInt16(_dt.Rows[0]["CallType"].ToString());

                        if (_callStatus == 100)
                        {
                            _strReturnMessage += "<br/><b style='color:red;'>" + Resources.ResultPage.SmsgThisCarCloased + "</b>";
                            _strReturnMessage += "&nbsp;<a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallAgain + "\");' href='offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + txtCarId.Text + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "'><b>[" + Resources.ResultPage.SmsgCallAgainNow + "]</b></a>";
                            lblNewCallMessage.Text = _strReturnMessage;
                        }
                        else
                        {
                            if (_callType == 101)
                            {
                                _strReturnMessage += Resources.ResultPage.SmsgType + " : <b style='color:red;'>" + Resources.ResultPage.SmsgCallMe + "</b>";
                                _strReturnMessage += Resources.ResultPage.SmsgTime + " : <b>" + _dt.Rows[0]["CallTime"].ToString() + "</b>";
                                _strReturnMessage += Resources.ResultPage.SmsgCompany + " : <b>" + _dt.Rows[0]["ProviderCompanyName"].ToString() + "</b>";
                                _strReturnMessage += Resources.ResultPage.SmsgRequestOn + " : <b>" + Convert.ToDateTime(_dt.Rows[0]["CallMeRequestDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b>";
                                _strReturnMessage += "&nbsp;<a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallNow + "\");' href='offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + txtCarId.Text + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "'><b>[" + Resources.ResultPage.SmsgCallNow + "]</b></a>";
                                lblNewCallMessage.Text = _strReturnMessage;
                            }
                            else if (_callType == 102)
                            {
                                _strReturnMessage += Resources.ResultPage.SmsgType + " : <b style='color:red;'>CALL ME BACK</b>";
                                _strReturnMessage += Resources.ResultPage.SmsgTime + " : <b>" + _dt.Rows[0]["CallTime"].ToString() + "</b>";
                                _strReturnMessage += Resources.ResultPage.SmsgCompany + " : <b>" + _dt.Rows[0]["ProviderCompanyName"].ToString() + "</b>";
                                _strReturnMessage += Resources.ResultPage.SmsgRequestOn + " : <b>" + Convert.ToDateTime(_dt.Rows[0]["CallMeRequestDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt") + "</b>";
                                _strReturnMessage += "&nbsp;<a onclick='javascript:return confirm(\"" + Resources.ResultPage.SmsgAreUsureCallNow + "\");' href='offline/ViewCINoMatchFound.aspx?" + "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + txtCarId.Text + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&callmeid=" + _dt.Rows[0]["CallMeID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString() + "'><b>[" + Resources.ResultPage.SmsgCallNow + "]</b></a>";
                                lblNewCallMessage.Text = _strReturnMessage;
                            }
                            else
                            {
                                _strReturnMessage = "leadid=" + _dt.Rows[0]["AgentLeadID"].ToString() + "&carid=" + txtCarId.Text + "&custid=" + _dt.Rows[0]["CustomerID"].ToString() + "&calltype=" + _dt.Rows[0]["CallType"].ToString();
                                Response.Redirect("offline/ViewCINoMatchFound.aspx?" + _strReturnMessage);
                            }
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void CheckApplicationStatus()
    {
        MyDatabase _db = new MyDatabase();
        SortedList _paraList = new SortedList();
        _paraList.Add("@CECarID", txtCarId.Text);
        _paraList.Add("@CustomerID", txtCustomerId.Text);
        DataTable _dtApp = new DataTable("tblApp");
        _dtApp = _db.GetDataTableBySP("sprocCXCRMNonGGApplicationStatusSelectItem", _paraList);

        if (_dtApp != null && _dtApp.Rows.Count > 0)
        {
            grvSelectedApp.DataSource = _dtApp;
            grvSelectedApp.DataBind();

            lblCarMoreDetailID.Text = _dtApp.Rows[0]["CECarMoreDetailId"].ToString();
            
            lblLastYearRefNo.Text = _dtApp.Rows[0]["QuoteRefNo"].ToString();

            lblAppStatusMessage.Text = string.Empty;
            lblAppStatusMessage.Text = Resources.General.NonGGFoundAppDetailSubmission;
            lblAppStatusMessage.ForeColor = System.Drawing.Color.Red;
            lblAppStatusMessage.Font.Bold = true;

            SetGVPremiumSelectedIndex();

            lblCarMoreDetailID.Visible = false;
            lblAppStatus.Visible = false;

            if (_dtApp.Rows[0]["Completed"].ToString() == "1")
            {
                divRenewalButtons.Visible = true;
                btnRenewQuote.Enabled = false;
                DateTime _dtModifiedDate = Convert.ToDateTime(_dtApp.Rows[0]["ModifiedDate"].ToString());

                if (Session[General.IsRenewalAgent].ToString() == "1")
                {
                    if (DateTime.Compare(dateTimeNow.AddMonths(-6), _dtModifiedDate) == 1)
                    {
                        btnRenewQuote.Enabled = true;
                    }
                    else
                    {
                        lblRenewalQuoteMessage.Text = "Too early to renew. Insurance Bought date at " +  _dtModifiedDate.ToString("dd-MMM-yyyy");
                        lblRenewalQuoteMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
                divResult.Visible = false;
                divCompletedQuoteDetails.Visible = false;
                btnNewCall.Visible = false;
            }

            
        }
        else
        {
            lblCarMoreDetailID.Text = "-1";
            lblAppStatus.Text = string.Empty;
          
            lblAppStatusMessage.Text = string.Empty;
            divRenewalButtons.Visible = false;
            btnNewCall.Visible = true;
        }
    }

    private void SetGVPremiumSelectedIndex()
    {
        foreach (GridViewRow myRow in grvPremiumList.Rows)
        {
            string _selectStatus = myRow.Cells[11].Text.ToString();
           
            if (_selectStatus == "Yes")
            {
                grvPremiumList.SelectedIndex = myRow.RowIndex;
                myRow.Cells[10].ForeColor = System.Drawing.Color.Red;
               
                break;
            }
            
        }
    }

    protected void ibtSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            lblCallStatusMessage.Text = "";
            divCustomerList.Visible = true;
            divCallMeCustomerList.Visible = false;
            divLinks.Visible = false;
            divResult.Visible = false;
            divCallStatus.Visible = false;
            divCustomerDetail.Visible = false;
            divPolicyDetails.Visible = false;
            divCompletedQuoteDetails.Visible = false;
            divCallHistory.Visible = false;
            divCarDetailList.Visible = false;
            divPremiumList.Visible = false;
            divRenewalButtons.Visible = false;
            lblMessage.Visible = true;
            lblMessage.Text = string.Empty;

            txtCarIdOnly.Text = "false";
            txtSearchCarIDHidden.Text = string.Empty;
            txtSearchTextHidden.Text = string.Empty;

            
            if (txtSearchRefNo.Text.Trim().Length <= 5 && txtSearchCarID.Text.Length <= 3 && txtSearch.Text.Trim().Length <= 3
                && txtLicensePlate.Text.Trim().Length <= 2)
            {
                lblMessage.Text = Resources.ResultPage.SmsgSearch + ": <b>" + Resources.General.Notvalid + "</b>";
                return;
            }
            else
            {
                lblMessage.Text = Resources.ResultPage.SmsgSearch + ": <b>" + txtSearch.Text + "</b>";
            }

            if (txtSearchRefNo.Text.Trim() != string.Empty && txtSearchRefNo.Text != "0")
            {
                string _tmpCarId = string.Empty;
                _tmpCarId = General.GetCarId(txtSearchRefNo.Text);
                txtSearchCarID.Text = _tmpCarId;
                lblMessage.Text = Resources.ResultPage.SmsgSearchByRefNo + ": <b>" + txtSearchRefNo.Text + "</b>";
            }
            else
            {
                txtSearchRefNo.Text = "0";
            }

            if (txtSearchCarID.Text.Trim() != string.Empty && txtSearchCarID.Text != "0")
            {
                Int64 _tmpCarID = 0;
                if (Int64.TryParse(txtSearchCarID.Text, out _tmpCarID))
                {
                    txtSearch.Text = _tmpCarID.ToString();
                    txtSearchCarID.Text = _tmpCarID.ToString();
                    txtCarIdOnly.Text = "true";
                    if (txtSearchRefNo.Text == "0")
                    {
                        lblMessage.Text = Resources.ResultPage.SmsgSearchByCarId + ": <b>" + txtSearchCarID.Text + "</b>";
                    }
                }
                else
                {
                    lblMessage.Text = Resources.ResultPage.SmsgInvlidCarID;// "Invlid Car ID.";
                    return;
                }
            }
            else
            {
                txtSearchCarID.Text = "0";
            }

            if (txtLicensePlate.Text.Trim() != string.Empty && txtLicensePlate.Text != "")
            {
                if (txtSearchRefNo.Text == "0" && txtSearchCarID.Text == "0")
                {
                    txtSearch.Text = "";
                    lblMessage.Text = Resources.ResultPage.SmsgSearch + " " + Resources.CarInsuranceResultPage.LicensePlate + ": <b>" + txtLicensePlate.Text + "</b>";
                }
            }
            else
            {
                txtLicensePlate.Text = "";
            }

            txtSearchCarIDHidden.Text = txtSearchCarID.Text;
            txtSearchTextHidden.Text = txtSearch.Text;

            txtCarId.Text = "";
            txtCustomerId.Text = "";
            txtCarPolicyId.Text = "";
            hplReRunQuote.NavigateUrl = "";
            hplNewQuote.NavigateUrl = "";

            gvCustomerList.DataSource = GetCustomerList();
            gvCustomerList.DataBind();

            if (gvCustomerList.Rows.Count > 0)
            {
                if (txtCarIdOnly.Text == "true")
                {
                    gvCustomerList.SelectedIndex = 0;
                    gvCustomerList_SelectedIndexChanged(null, null);
                }
                else
                {
                    if (gvCustomerList.Rows.Count == 1)
                    {
                        gvCustomerList.SelectedIndex = 0;
                        gvCustomerList_SelectedIndexChanged(null, null);
                    }
                    else
                    {
                        gvCustomerList.SelectedIndex = -1;
                    }
                }


                lblMessage.Text += " ( " + gvCustomerList.Rows.Count.ToString() + " record(s) found.)";
                
            }

            //28March2014
            GetPromotion();

            txtSearchRefNo.Text = string.Empty;
            txtSearchCarID.Text = string.Empty;
            txtSearch.Text = string.Empty;
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Resources.ResultPage.SmsgPleaseTryAgain + "<br/>" + Resources.ResultPage.SmsgSearchWordShort + "<br/> " + Ex.Message;
        }
    }

    private DataTable GetCustomerList()
    {
        MyDatabase _db = new MyDatabase();
        SortedList _spara = new SortedList();
        _spara.Add("@SearchKey", txtSearchTextHidden.Text);
        _spara.Add("@CarIDOnly", txtCarIdOnly.Text);
        _spara.Add("@UsrTypeID", ddlUserTypeID.SelectedValue);
        _spara.Add("@AgentID", txtAgentId.Text);
        _spara.Add("@SearchBy", ddlSearchBy.SelectedValue);
        _spara.Add("@CarPlate", txtLicensePlate.Text);
        DataTable _dt = _db.GetDataTableBySP("sprocCXCRMSearchCustomer_TEST", _spara);
        return _dt;
    }

    #region Promotion

    private void GetPromotion()
    {
        divPromotion.Visible = false;
        ltrPromotion.Text = "";
        if (txtCustomerId.Text != "")
        {
            PromotionGGDB objPro = new PromotionGGDB();
            DataTable _dt = objPro.GetPromotionByCustomer(Int64.Parse(txtCustomerId.Text));
            if (_dt.Rows.Count > 0)
            {
                ltrPromotion.Text = "<table border=\"0\" style=\"width:100%\">";
                foreach (DataRow drPro in _dt.Rows)
                {
                    ltrPromotion.Text += "<tr>";
                    ltrPromotion.Text += "<td>Promotion:</td><td colspan=\"5\"><b style='color:red;'>" + drPro["PromotionName"].ToString() + "</b></td>";
                    System.Globalization.CultureInfo cul = new System.Globalization.CultureInfo("th-TH");
                    ltrPromotion.Text += "</tr>";
                    ltrPromotion.Text += "<tr>";
                    ltrPromotion.Text += "<td>" + Resources.CarInsurance.CoverCommencementDate + ":</td><td><b style='color:blue;'>" + cul.DateTimeFormat.GetMonthName(int.Parse(drPro["CommencementMonth"].ToString())) + "</b></td>";
                    ltrPromotion.Text += "<td>" + Resources.CarInsurance.CallTime + ":</td><td><b style='color:green;'>" + GetCallAt(int.Parse(drPro["CallAt"].ToString())) + "</b></td>";
                    ltrPromotion.Text += "<td>" + Resources.General.Requestedon + ":</td><td><b style='color:red;'>" + Convert.ToDateTime(drPro["CreatedDate"].ToString()).ToString("dd/MMM/yyyy hh:mm:ss tt") + "</b></td>";
                    ltrPromotion.Text += "</tr>";
                }
                ltrPromotion.Text += "</table>";
                divPromotion.Visible = true;
            }
        }
    }

    private string GetCallAt(int callAt)
    {
        switch (callAt)
        {
            case 1:
                return "ตอนเช้า";
            case 2:
                return "ตอนบ่าย";
            case 3:
                return "ตอนเย็น";
            case 4:
                return "ทุกเวลา";
            default:
                return "";
        }
    }

    #endregion

    protected void csvTargetDate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (rdoCallBack.Checked)
        {
            string _strTargetDateTime = ddlTDay.Text + "/" + ddlTMonth.SelectedItem.Text + "/" + ddlTYear.Text + " " + ddlTHour.Text + ":" + ddlTMin.Text;
            DateTime _dtTargetDateTime = dateTimeNow;

            if (DateTime.TryParse(_strTargetDateTime, out _dtTargetDateTime))
            {
                txtCallBackDateTime.Text = _dtTargetDateTime.ToString("dd/MMM/yyyy hh:mm:ss tt");
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
                txtCallBackDateTime.Text = "0";
            }
        }
    }

    protected void ddlCIMainCallStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCIMainCallStatus.Items.Count > 0 && ddlCIMainCallStatus.SelectedIndex >= 0)
        {
            LoadCallStatusList(Convert.ToInt32(ddlCIMainCallStatus.Text));
        }
    }

    private void LoadMainCallStatusList(int intType)
    {
        try
        {
            MyDatabase _db = new MyDatabase();
            string _strSQL = "Select * from tblCXCRMMainCallStatusType Where [Status] = 1 And [Type] = " + intType.ToString();

            DataTable _dtList = new DataTable("tblList");
            _dtList = _db.GetDataTable(_strSQL);
            ddlCIMainCallStatus.Items.Clear();
            ddlCICallStatus.Items.Add(new ListItem("Select", "-1"));
            if (_dtList != null && _dtList.Rows.Count > 0)
            {
                for (int i = 0; i < _dtList.Rows.Count; i++)
                {
                    ddlCIMainCallStatus.Items.Add(new ListItem(_dtList.Rows[i]["EN"].ToString(), _dtList.Rows[i]["MainCallStatusTypeID"].ToString()));
                }
            }
        }
        catch (Exception Ex)
        {
            throw;
        }
    }

    private void LoadCallStatusList(int MainStatusID)
    {
        try
        {
            MyDatabase _db = new MyDatabase();
            string _strSQL = @"Select CallStatusTypeID, 
                                '[' + CAST(StatusCode AS VARCHAR) + '] ' + EN AS EN, 
                                '[' + CAST(StatusCode AS VARCHAR) + '] ' + TH AS TH
                                FROM tblCXCRMCallStatusType Where [Status] = 1 And [MainCallStatusTypeID] = " + MainStatusID.ToString();
            DataTable _dtList = new DataTable("tblList");
            _dtList = _db.GetDataTable(_strSQL);
            ddlCICallStatus.Items.Clear();
            ddlCICallStatus.Items.Add(new ListItem("Select", "-1"));
            if (_dtList != null && _dtList.Rows.Count > 0)
            {
                for (int i = 0; i < _dtList.Rows.Count; i++)
                {
                    ddlCICallStatus.Items.Add(new ListItem(_dtList.Rows[i]["EN"].ToString(), _dtList.Rows[i]["CallStatusTypeID"].ToString()));
                }
            }
        }
        catch (Exception Ex)
        {
            throw;
        }
    }

    protected void rdoCallFinish_CheckedChanged(object sender, EventArgs e)
    {
        trCallBack1.Visible = false;
        trCallBack2.Visible = false;
        if (rdoCallFinish.Checked)
        {
            LoadMainCallStatusList(1);
        }
        else if (rdoCallBack.Checked)
        {
            trCallBack1.Visible = true;
            trCallBack2.Visible = true;
            LoadMainCallStatusList(2);
        }
        else if (rdoFollowUp.Checked)
        {
            LoadMainCallStatusList(3);
        }

        if (ddlCIMainCallStatus.Items.Count > 0)
        {
            ddlCIMainCallStatus.SelectedIndex = 0;
            ddlCIMainCallStatus_SelectedIndexChanged(sender, e);
        }
    }

    protected void gvCarDetailList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (gvCarDetailList.SelectedIndex >= 0)
        {
            divBuyNowStatus.Visible = false;
            lblBuyNowStatus.Text = "";
            lblNewCallMessage.Text = "";
            GridViewRow _row = gvCarDetailList.SelectedRow;
            ShowCarAndCustomerDetails(Convert.ToInt64(txtCustomerId.Text), Convert.ToInt64(_row.Cells[0].Text), Convert.ToInt64(_row.Cells[1].Text));
            hplOldQuote.NavigateUrl = "old-quotes.aspx?custid=" + txtCustomerId.Text + "&carid=" + _row.Cells[0].Text;
            gvCallHistory.DataBind();

            if (Request.Params["page"] != null && Request.Params["page"].ToString().ToUpper() == "OLDQUOTE")
            {
                btnNewCall.Visible = false;
            }
            
        }
    }

    protected void gvCallHistory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            
            Int64 value = Convert.ToInt64(e.Row.Cells[2].Text);
            
            if (value.ToString() == txtCarId.Text)
            {
                e.Row.Cells[2].BackColor = System.Drawing.Color.FromName("#c6c6c6");
                e.Row.Cells[2].Font.Bold = true;
                e.Row.Cells[2].Font.Size = FontUnit.Medium;

                e.Row.Cells[4].BackColor = System.Drawing.Color.FromName("#c6c6c6");
                e.Row.Cells[4].Font.Bold = true;
               
                e.Row.BackColor = System.Drawing.Color.FromName("#ffffcc");

                e.Row.Height = Unit.Pixel(Convert.ToInt32(e.Row.Height.Value + 60d));
                
            }
            
        }
    }

    //Call Type 102 and 103 Process
    protected void btnSerach_Click(object sender, EventArgs e)
    {
        try
        {
            if (lblCallType.Text != string.Empty && lblCallType.Text == "103")
            {
                lblAgentLeadID.Text = "0";
                txtCarId.Text = "0";
                txtCustomerId.Text = "0";
                txtCallMeId.Text = "0";
            }
            btnNewCallMeCall.Visible = false;
            lblCallStatusMessage.Text = "";
            divCustomerList.Visible = true;
            divCallMeCustomerList.Visible = true;
            divLinks.Visible = false;
            divResult.Visible = false;
            divCallStatus.Visible = false;
            divCustomerDetail.Visible = false;
            divPolicyDetails.Visible = false;
            divCompletedQuoteDetails.Visible = false;
            divCallHistory.Visible = false;
            divCarDetailList.Visible = false;
            divRenewalButtons.Visible = false;
            divPremiumList.Visible = false;
            lblMessage.Visible = true;
            lblMessage.Text = string.Empty;

            txtCarIdOnly.Text = "false";
            txtSearchCarIDHidden.Text = string.Empty;
            txtSearchTextHidden.Text = string.Empty;
            txtOldCustomerId.Text = string.Empty;

            if (txtSearchRefNo.Text.Trim() != string.Empty && txtSearchRefNo.Text != "0")
            {
                string _tmpCarId = string.Empty;
                _tmpCarId = General.GetCarId(txtSearchRefNo.Text);
                txtSearchCarID.Text = _tmpCarId;
            }
            else
            {
                txtSearchRefNo.Text = "0";
            }

            if (txtSearchCarID.Text.Trim() != string.Empty && txtSearchCarID.Text != "0")
            {
                Int64 _tmpCarID = 0;
                if (Int64.TryParse(txtSearchCarID.Text, out _tmpCarID))
                {
                    txtSearch.Text = _tmpCarID.ToString();
                    txtSearchCarID.Text = _tmpCarID.ToString();
                    txtCarIdOnly.Text = "true";
                }
            }
            else
            {
                txtSearchCarID.Text = "0";
            }

            txtSearchCarIDHidden.Text = txtSearchCarID.Text;
            txtSearchTextHidden.Text = txtSearch.Text;

            if (ddlSearchBy.Text == "MOBILE")
            {
                if (txtMobileNoSearch.Text.Trim().Length > 5)
                {
                    lblMessage.Text = Resources.ResultPage.SmsgSearchByMobileNo + ": <b>" + txtMobileNoSearch.Text + "</b>";
                }
                else
                {
                    lblMessage.Text = Resources.ResultPage.SmsgSearchByMobileNo + ": <b>" + Resources.ResultPage.SmsgEnterValidMobileNo + "</b>";
                    return;
                }
            }
            else if (ddlSearchBy.Text == "NAME")
            {
                if (txtMobileNoSearch.Text.Trim().Length >= 3)
                {
                    lblMessage.Text = Resources.ResultPage.SmsgSearchByName + ": <b>" + txtMobileNoSearch.Text + "</b>";
                }
                else
                {
                    lblMessage.Text = Resources.ResultPage.SmsgSearchByName + ": <b>" + Resources.ResultPage.SmsgEnterValidName + "</b>";
                    return;
                }
            }
            else if (ddlSearchBy.Text == "EMAIL")
            {
                if (txtMobileNoSearch.Text.Trim().Length > 6)
                {
                    lblMessage.Text = Resources.ResultPage.SmsgSearchByEmail + ": <b>" + txtMobileNoSearch.Text + "</b>";
                }
                else
                {
                    lblMessage.Text = Resources.ResultPage.SmsgSearchByEmail + ": <b>" + Resources.ResultPage.SmsgEnterValidEmail + "</b>";
                    return;
                }
            }
            else if (ddlSearchBy.Text == "LICENSE")
            {
                if (txtMobileNoSearch.Text.Trim().Length >= 3)
                {
                    txtLicensePlate.Text = txtMobileNoSearch.Text;
                    lblMessage.Text = Resources.ResultPage.SmsgSearch + " " + Resources.CarInsuranceResultPage.LicensePlate + ": <b>" + txtMobileNoSearch.Text + "</b>";
                }
                else
                {
                    lblMessage.Text = Resources.ResultPage.SmsgSearch + " " + Resources.CarInsuranceResultPage.LicensePlate + ": <b>ต้องมากกว่า 3 ตัวอักษร</b>";
                    return;
                }
            }

            if (ddlSearchBy.Text != "LICENSE")  txtSearchTextHidden.Text = txtMobileNoSearch.Text;

            txtCarId.Text = "";
            txtCustomerId.Text = "";
            txtCarPolicyId.Text = "";
            hplReRunQuote.NavigateUrl = "";
            hplNewQuote.NavigateUrl = "";

            gvCustomerList.DataSource = GetCustomerList();
            gvCustomerList.DataBind();
            txtLicensePlate.Text = "";

            if (gvCustomerList.Rows.Count > 0)
            {
                lblMessage.Text += " ( " + gvCustomerList.Rows.Count.ToString() + " " + Resources.ResultPage.SmsgRecordFound + ")";
            }
            if (Request.Params["callmeid"] != null && Request.Params["callmeid"].ToString() != "0")
            {
                LoadCallMeCustomerList(Convert.ToInt64(Request.Params["callmeid"].ToString()));
            }
            else
            {
                LoadCallMeCustomerList();
            }
            if (gvCustomerList.Rows.Count > 0)
            {
                hplOldQuote.Visible = true;
                if (txtCarIdOnly.Text == "true")
                {
                    gvCustomerList.SelectedIndex = 0;
                    gvCustomerList_SelectedIndexChanged(null, null);
                }
                else
                {
                    if (gvCustomerList.Rows.Count == 1)
                    {
                        gvCustomerList.SelectedIndex = 0;
                        gvCustomerList_SelectedIndexChanged(null, null);
                    }
                    else
                    {
                        gvCustomerList.SelectedIndex = -1;
                    }
                }
            }
            else
            {
                hplOldQuote.Visible = false;
            }

            if (gvCallMeCustomerList.Rows.Count > 0)
            {
                lblMessage.Text += " ( " + gvCallMeCustomerList.Rows.Count.ToString() + " " + Resources.ResultPage.SmsgCallMeRecordFound + ")";

            }

            txtSearchRefNo.Text = string.Empty;
            txtSearchCarID.Text = string.Empty;
            txtSearch.Text = string.Empty;
            if (Request.Params["calltype"] == "103")
            {
                btnNewLead.Visible = true;
                btnClaim.Visible = true;
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Resources.ResultPage.SmsgPleaseTryAgain + "Please try again later.<br/>" + Resources.ResultPage.SmsgSearchWordShort + "<br/> " + Ex.Message;
        }
    }

    protected void btnNewLead_Click(object sender, EventArgs e)
    {
        try
        {
            Int64 _agentLeadId = 0;
            Int64 _customerId = 0;
            Int64 _carId = 0;
            Int64 _callMeId = 0;
            Int64 _agentID = 0;
            if (!Int64.TryParse(txtAgentId.Text, out _agentID))
            {
                _agentID = 0;
            }

            LoadCallMeAgentLeads(_agentLeadId, _customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), 103, _callMeId, out _agentLeadId, out _customerId, out _carId);

            if (_agentLeadId > 0)
            {
                string _strParameters = "leadid=" + _agentLeadId.ToString() + "&calltype=" + lblCallType.Text;
                _strParameters += "&custid=" + _customerId.ToString() + "&carid=" + _carId.ToString();

                if (_callMeId > 0)
                {
                    _strParameters += "&callmeid=" + _callMeId.ToString();
                }

                if (_customerId > 0 && _carId > 0)
                {
                    Response.Redirect("~/carinsurance/offline/ViewCINoMatchFound.aspx?" + _strParameters.ToString());
                }
                else
                {
                    if (ddlSearchBy.Text == "MOBILE")
                    {
                        Response.Redirect("~/carinsurance/offline/callme-newlead-quotes.aspx?" + _strParameters.ToString() + "&mobile=" + txtMobileNoSearch.Text);
                    }
                    else if (ddlSearchBy.Text == "NAME")
                    {
                        Response.Redirect("~/carinsurance/offline/callme-newlead-quotes.aspx?" + _strParameters.ToString() + "&name=" + txtMobileNoSearch.Text);
                    }
                    else if (ddlSearchBy.Text == "EMAIL")
                    {
                        Response.Redirect("~/carinsurance/offline/callme-newlead-quotes.aspx?" + _strParameters.ToString() + "&email=" + txtMobileNoSearch.Text);
                    }
                    else
                    {
                        Response.Redirect("~/carinsurance/offline/callme-newlead-quotes.aspx?" + _strParameters.ToString() + "&email=" + txtMobileNoSearch.Text);
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            lblNewCallMessage.Text = Ex.ToString();
        }
    }

    protected void btnNewCar_Click(object sender, EventArgs e)
    {
        try
        {
            Int64 _agentLeadId = 0;
            Int64 _customerId = 0;
            Int64 _carId = 0;
            Int64 _callMeId = 0;
            Int64 _agentID = 0;
            if (!Int64.TryParse(txtAgentId.Text, out _agentID))
            {
                _agentID = 0;
            }

            LoadCallMeAgentLeads(_agentLeadId, _customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), 103, _callMeId, out _agentLeadId, out _customerId, out _carId);

            if (_agentLeadId > 0)
            {
                string _strParameters = "";
                if (!Int64.TryParse(txtOldCustomerId.Text, out _customerId))
                {
                    _customerId = 0;
                }

                _strParameters = "leadid=" + _agentLeadId.ToString() + "&calltype=" + lblCallType.Text;
                _strParameters += "&custid=" + _customerId.ToString() + "&carid=0";

                if (_customerId > 0)
                {
                    Response.Redirect("~/carinsurance/offline/callme-newlead-quotes.aspx?" + _strParameters.ToString());
                }
                
            }
        }
        catch (Exception Ex)
        {
            lblNewCallMessage.Text = Ex.ToString();
        }
    }

    protected void btnClaim_Click(object sender, EventArgs e)
    {
        if (ddlSearchBy.Text == "MOBILE")
        {
            Response.Redirect("~/carinsurance/offline/ClaimCall.aspx?mobile=" + txtMobileNoSearch.Text);
        }
        else if (ddlSearchBy.Text == "NAME")
        {
            Response.Redirect("~/carinsurance/offline/ClaimCall.aspx?name=" + txtMobileNoSearch.Text);
        }
        else if (ddlSearchBy.Text == "EMAIL")
        {
            Response.Redirect("~/carinsurance/offline/ClaimCall.aspx?email=" + txtMobileNoSearch.Text);
        }
    }

    protected void btnRenewQuote_Click(object sender, EventArgs e)
    {
        try
        {
            Int64 _agentLeadId = 0;

            if (!Int64.TryParse(lblAgentLeadID.Text, out _agentLeadId))
            {
                _agentLeadId = 0;
            }

            Int64 _customerId = 0;

            if (!Int64.TryParse(txtCustomerId.Text, out _customerId))
            {
                _customerId = 0;
            }

            Int64 _carId = 0;
            Int64 _outCarID = 0;

            if (!Int64.TryParse(txtCarId.Text, out _carId))
            {
                _carId = 0;
            }

            

            Int64 _agentID = 0;

            if (!Int64.TryParse(txtAgentId.Text, out _agentID))
            {
                _agentID = 0;
            }

            LoadRenewalAgentLeads(_customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), 100, out _agentLeadId, out _customerId, out _outCarID);

            if (_agentLeadId > 0 && _outCarID > 0)
            {
                string _strParameters = "leadid=" + _agentLeadId.ToString() + "&calltype=100";

                

                _strParameters += "&renew=2";
                _strParameters += "&custid=" + _customerId.ToString() + "&carid=" + _outCarID.ToString();

                _strParameters += "&refno=" + lblLastYearRefNo.Text;
                

                if (_customerId > 0 && _carId > 0)
                {
                    Response.Redirect("~/carinsurance/offline/ViewCINoMatchFound.aspx?" + _strParameters.ToString());

                    
                }
                else
                {
                    lblRenewalQuoteMessage.Text = "Please try again later.";
                }
            }
        }
        catch (Exception Ex)
        {
            lblNewCallMessage.Text = Ex.ToString();
        }
    }

    public bool CheckCXBrokerPremiumCount(Int64 CarID, Int64 CustomerID)
    {
        bool _Result = false;
        MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
        SortedList _sPara = new SortedList();
        string _spName = "[sprocCXBrokerPremiumGetCount]";
        _sPara.Add("@CarID", CarID);
        _sPara.Add("@CustomerID", CustomerID);
        _sPara.Add("@ProductType", "CIN");

        object _objCount = _db.ExecuteScalarSP(_spName, _sPara);
        if (_objCount != null && Convert.ToInt32(_objCount) > 0)
        {
            _Result = true;
        }

        return _Result;
    }

    protected void btnNewCallMeCall_Click(object sender, EventArgs e)
    {
        try
        {
            Int64 _agentLeadId = 0;

            if (!Int64.TryParse(lblAgentLeadID.Text, out _agentLeadId))
            {
                _agentLeadId = 0;
            }

            Int64 _customerId = 0;

            if (!Int64.TryParse(txtCustomerId.Text, out _customerId))
            {
                _customerId = 0;
            }

            Int64 _carId = 0;

            if (!Int64.TryParse(txtCarId.Text, out _carId))
            {
                _carId = 0;
            }

            Int64 _callMeId = 0;

            if (!Int64.TryParse(txtCallMeId.Text, out _callMeId))
            {
                _callMeId = 0;
            }

            Int64 _agentID = 0;

            if (!Int64.TryParse(txtAgentId.Text, out _agentID))
            {
                _agentID = 0;
            }
            string _searchParam = "";
            if (lblCallType.Text != string.Empty && lblCallType.Text == "103")
            {
                if (ddlSearchBy.Text == "MOBILE")
                {
                    _searchParam = "&mobile=" + txtMobileNoSearch.Text;
                }
                else if (ddlSearchBy.Text == "NAME")
                {
                    _searchParam = "&name=" + txtMobileNoSearch.Text;
                }
                else if (ddlSearchBy.Text == "EMAIL")
                {
                    _searchParam = "&email=" + txtMobileNoSearch.Text;
                }
            }

            if (gvCallMeCallHistory.Rows.Count <= 0)
            {

                if (_callMeId > 0)
                {
                    LoadCallMeAgentLeads(_agentLeadId, _customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), 103, _callMeId, out _agentLeadId, out _customerId, out _carId);
                }
                else
                {
                    LoadCallMeAgentLeads(_agentLeadId, _customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), 103, 0, out _agentLeadId, out _customerId, out _carId);
                }


                if (_agentLeadId > 0)
                {
                    string _strParameters = "leadid=" + _agentLeadId.ToString() + "&calltype=" + lblCallType.Text;
                    _strParameters += "&custid=" + _customerId.ToString() + "&carid=" + _carId.ToString();

                    if (_callMeId > 0)
                    {
                        _strParameters += "&callmeid=" + _callMeId.ToString();
                    }

                    if (_customerId > 0 && _carId > 0)
                    {
                        Response.Redirect("~/carinsurance/offline/ViewCINoMatchFound.aspx?" + _strParameters.ToString());
                    }
                    else
                    {
                        Response.Redirect("~/carinsurance/offline/callme-newlead-quotes.aspx?" + _strParameters.ToString() + _searchParam); //"&mobile=" + txtMobileNoSearch.Text);
                    }
                }
            }
            else
            {
                if (_callMeId > 0)
                {
                    LoadCallMeAgentLeads(_agentLeadId, _customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), Convert.ToInt32(lblCallType.Text), _callMeId, out _agentLeadId, out _customerId, out _carId);
                }
                else
                {
                    LoadCallMeAgentLeads(_agentLeadId, _customerId, _carId, Convert.ToInt64(Session[CE.Global.General.UserId].ToString()), 103, 0, out _agentLeadId, out _customerId, out _carId);
                }

                lblNewCallMeCallMessage.Text = string.Empty;

                if (_agentLeadId > 0)
                {
                    string _strParameters = "leadid=" + _agentLeadId.ToString() + "&calltype=" + lblCallType.Text;
                    _strParameters += "&custid=" + _customerId.ToString() + "&carid=" + _carId.ToString();

                    if (_callMeId > 0)
                    {
                        _strParameters += "&callmeid=" + _callMeId.ToString();
                    }

                    if (_customerId > 0 && _carId > 0)
                    {
                        Response.Redirect("~/carinsurance/offline/ViewCINoMatchFound.aspx?" + _strParameters.ToString());
                    }
                    else
                    {
                        Response.Redirect("~/carinsurance/offline/callme-newlead-quotes.aspx?" + _strParameters.ToString() + _searchParam);// "&mobile=" + txtMobileNoSearch.Text);
                    }
                }

                
            }
        }
        catch (Exception Ex)
        {
            lblNewCallMessage.Text = Ex.ToString();
        }
    }

    protected void gvCallMeCustomerList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lblCallType.Text != string.Empty && lblCallType.Text == "103")
        {
            if (gvCallMeCustomerList.SelectedIndex >= 0)
            {
                lblNewCallMeCallMessage.Text = string.Empty;
                divCallMeCallHistory.Visible = true;
                divBuyNowStatus.Visible = false;
                lblBuyNowStatus.Text = "";
                lblNewCallMessage.Text = "";
                GridViewRow _row = gvCallMeCustomerList.SelectedRow;

                MyDatabase _db = new MyDatabase();
                string _strSQL = "SELECT CallMeId, AgentLeadId,CallType  from tblCXCRMAgentLeads Where CallMeId = " + Convert.ToInt64(_row.Cells[0].Text);
                txtCallMeId.Text = _row.Cells[0].Text;
                DataTable _dtList = new DataTable("tblList");
                _dtList = _db.GetDataTable(_strSQL);
                ddlCIMainCallStatus.Items.Clear();
                ddlCICallStatus.Items.Add(new ListItem(Resources.CarInsurance.Select, "-1"));
                if (_dtList != null && _dtList.Rows.Count > 0)
                {
                    lblAgentLeadID.Text = _dtList.Rows[0][1].ToString();
                    gvCallMeCallHistory.DataBind();
                }
                else
                {
                    lblAgentLeadID.Text = "0";
                    gvCallMeCallHistory.DataBind();
                }
                if (gvCallMeCustomerList.Rows.Count > 0)
                {
                    btnNewCallMeCall.Visible = true;
                }
                else
                {
                    btnNewCallMeCall.Visible = false;
                }
            }
            else
            {
                txtCallMeId.Text = "0";
            }
        }
        else if (lblCallType.Text != string.Empty && lblCallType.Text == "102")
        {
            if (gvCallMeCustomerList.SelectedIndex >= 0)
            {
                if (gvCallMeCustomerList.Rows.Count > 0)
                {
                    btnNewCallMeCall.Visible = true;
                }
                else
                {
                    btnNewCallMeCall.Visible = false;
                }
            }
        }
    }

    protected void gvCallMeCallHistory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            
            Int64 value = Convert.ToInt64(e.Row.Cells[2].Text);
            
            if (value.ToString() == txtCarId.Text)
            {
                e.Row.Cells[2].BackColor = System.Drawing.Color.FromName("#c6c6c6");
                e.Row.Cells[2].Font.Bold = true;
                e.Row.Cells[2].Font.Size = FontUnit.Medium;

                e.Row.Cells[4].BackColor = System.Drawing.Color.FromName("#c6c6c6");
                e.Row.Cells[4].Font.Bold = true;
                
                e.Row.BackColor = System.Drawing.Color.FromName("#ffffcc");

                e.Row.Height = Unit.Pixel(Convert.ToInt32(e.Row.Height.Value + 60d));
                
            }
        }
    }

    private bool ShowScreen(string CallTypeId)
    {
        bool _retValue = false;
        try
        {
            string _spName = "sprocCXCRMGetCarDetailSelectSingleItem";
            MyDatabase _db = new MyDatabase();
            DataTable _dtCarDetail = new DataTable("tblCar");
            SortedList _paraList = new SortedList();
            _paraList.Add("@AgentID", Session[General.UserId].ToString());
            _paraList.Add("@CallType", CallTypeId);
            _paraList.Add("@LangOption", lblLangOption.Text);

            _dtCarDetail = _db.GetDataTableBySP(_spName, _paraList);
            if (_dtCarDetail != null && _dtCarDetail.Rows.Count > 0)
            {
                lblAgentLeadID.Text = _dtCarDetail.Rows[0][0].ToString();
                txtCallMeId.Text = _dtCarDetail.Rows[0][5].ToString();
                if (CallTypeId.ToString() != string.Empty && CallTypeId.ToString() == "102")
                {
                    txtCustomerId.Text = _dtCarDetail.Rows[0][1].ToString();
                    txtCarId.Text = _dtCarDetail.Rows[0][2].ToString();
                }
                _retValue = true;
            }
            else
            {
                _retValue = false;
            }
        }
        catch (Exception Ex)
        {
            _retValue = false;
            lblCallSessionName.Text += "<br/><b style='color:red;'>" + Resources.General.ERROR + "</b>: at ShowScreen()<br/>" + Ex.Message + "<br/><br/>" + Ex.StackTrace;
        }
        return _retValue;
    }

    private void LoadCallMeCustomerList()
    {
        string _spName = "sprocCXCRMSearchCallMeCustomer";
        MyDatabase _db = new MyDatabase();
        DataTable _dtCallMeCustomer = new DataTable("tblCallMeCust");
        SortedList _paraList = new SortedList();
        _paraList.Add("@SearchKey", txtSearchTextHidden.Text);
        _paraList.Add("@SearchBy", ddlSearchBy.Text);
        _dtCallMeCustomer = _db.GetDataTableBySP(_spName, _paraList);
        if (_dtCallMeCustomer != null && _dtCallMeCustomer.Rows.Count > 0)
        {
            gvCallMeCustomerList.DataSource = _dtCallMeCustomer;
            gvCallMeCustomerList.DataBind();
            if (lblCallType.Text != string.Empty && lblCallType.Text == "102")
            {
                if (gvCallMeCustomerList.Rows.Count > 0)
                {
                    gvCallMeCustomerList.SelectedIndex = 0;
                    gvCallMeCustomerList_SelectedIndexChanged(null, null);
                    divCallMeCustomerList.Visible = true;
                    divCallMeCallHistory.Visible = true;
                    btnNewCallMeCall.Visible = true;
                }
                else
                {
                    divCallMeCustomerList.Visible = false;
                    divCallMeCallHistory.Visible = false;
                    btnNewCallMeCall.Visible = false;
                }
            }
            else
            {
                if (gvCallMeCustomerList.Rows.Count > 0)
                {
                    gvCallMeCustomerList.SelectedIndex = -1;
                    gvCallMeCustomerList_SelectedIndexChanged(null, null);
                    divCallMeCustomerList.Visible = true;
                    divCallMeCallHistory.Visible = true;
                    btnNewCallMeCall.Visible = true;
                }
            }
        }
        else
        {
            gvCallMeCustomerList.DataSource = null;
            gvCallMeCustomerList.DataBind();
            divCallMeCustomerList.Visible = false;
            divCallMeCallHistory.Visible = false;
            btnNewCallMeCall.Visible = false;
        }

       
    }

    private void LoadCallMeCustomerList(Int64 CallMeId)
    {
        string _spName = "sprocCXCRMSearchCallMeCustomer";
        MyDatabase _db = new MyDatabase();
        DataTable _dtCallMeCustomer = new DataTable("tblCallMeCust");
        SortedList _paraList = new SortedList();
        _paraList.Add("@SearchKey", txtSearchTextHidden.Text);
        _paraList.Add("@SearchBy", ddlSearchBy.Text);
        _paraList.Add("@CallMeId", CallMeId);
        _dtCallMeCustomer = _db.GetDataTableBySP(_spName, _paraList);
        if (_dtCallMeCustomer != null && _dtCallMeCustomer.Rows.Count > 0)
        {
            gvCallMeCustomerList.DataSource = _dtCallMeCustomer;
            gvCallMeCustomerList.DataBind();
            if (lblCallType.Text != string.Empty && lblCallType.Text == "102")
            {
                if (gvCallMeCustomerList.Rows.Count > 0)
                {
                    gvCallMeCustomerList.SelectedIndex = 0;
                    gvCallMeCustomerList_SelectedIndexChanged(null, null);
                    divCallMeCustomerList.Visible = true;
                    divCallMeCallHistory.Visible = true;
                    btnNewCallMeCall.Visible = true;
                }
                else
                {
                    divCallMeCustomerList.Visible = false;
                    divCallMeCallHistory.Visible = false;
                    btnNewCallMeCall.Visible = false;
                }
            }
            else
            {
                if (gvCallMeCustomerList.Rows.Count > 0)
                {
                    gvCallMeCustomerList.SelectedIndex = -1;
                    gvCallMeCustomerList_SelectedIndexChanged(null, null);
                    divCallMeCustomerList.Visible = true;
                    divCallMeCallHistory.Visible = true;
                    btnNewCallMeCall.Visible = true;
                }
            }
        }
        else
        {
            gvCallMeCustomerList.DataSource = null;
            gvCallMeCustomerList.DataBind();
            divCallMeCustomerList.Visible = false;
            divCallMeCallHistory.Visible = false;
            btnNewCallMeCall.Visible = false;
        }

        
    }

    private void LoadCallMeAgentLeads(Int64 AgentLeadId, Int64 CustomerID, Int64 CarId, Int64 AgentId, Int32 CallTypeId, Int64 CallMeId, out Int64 outAgentLeadID, out Int64 outCustomerID, out Int64 outCarID)
    {
        string _spName = "sprocCXCRMAgentLeadDetailsSelectSingleItem";
        MyDatabase _db = new MyDatabase();
        DataTable _dtCallMeAgentLeads = new DataTable("tblCallMeAgentLeads");
        SortedList _paraList = new SortedList();
        _paraList.Add("@AgentLeadId", AgentLeadId);
        _paraList.Add("@CustomerID", CustomerID);
        _paraList.Add("@CarID", CarId);
        _paraList.Add("@AgentId", AgentId);
        _paraList.Add("@CallTypeId", CallTypeId);
        _paraList.Add("@CallMeId", CallMeId);
        _dtCallMeAgentLeads = _db.GetDataTableBySP(_spName, _paraList);

        outAgentLeadID = 0;
        outCustomerID = 0;
        outCarID = 0;
        if (_dtCallMeAgentLeads != null && _dtCallMeAgentLeads.Rows.Count > 0)
        {
            lblAgentLeadID.Text = _dtCallMeAgentLeads.Rows[0]["AgentLeadId"].ToString();
            txtCustomerId.Text = _dtCallMeAgentLeads.Rows[0]["CustomerId"].ToString();
            txtCarId.Text = _dtCallMeAgentLeads.Rows[0]["CarId"].ToString();
            txtCallMeId.Text = _dtCallMeAgentLeads.Rows[0]["CallMeId"].ToString();
            lblCallType.Text = _dtCallMeAgentLeads.Rows[0]["CallType"].ToString();

            outAgentLeadID = Convert.ToInt64(lblAgentLeadID.Text);
            outCustomerID = Convert.ToInt64(txtCustomerId.Text);
            outCarID = Convert.ToInt64(txtCarId.Text);
        }
        
    }

    private void LoadRenewalAgentLeads(Int64 CustomerID, Int64 CarId, Int64 AgentId, Int32 CallType, out Int64 outAgentLeadID, out Int64 outCustomerID, out Int64 outCarID)
    {
        string _spName = "sprocCXCRMAgentRenewalLeadDetailsSelectSingleItem";
        MyDatabase _db = new MyDatabase();
        DataTable _dtCallMeAgentLeads = new DataTable("tblRenealAgentLeads");
        SortedList _paraList = new SortedList();
        _paraList.Add("@CustomerID", CustomerID);
        _paraList.Add("@CarID", CarId);
        _paraList.Add("@CallType", CallType);
        _paraList.Add("@AgentId", AgentId);
        _dtCallMeAgentLeads = _db.GetDataTableBySP(_spName, _paraList);

        outAgentLeadID = 0;
        outCustomerID = 0;
        outCarID = 0;
        if (_dtCallMeAgentLeads != null && _dtCallMeAgentLeads.Rows.Count > 0)
        {
            lblAgentLeadID.Text = _dtCallMeAgentLeads.Rows[0]["AgentLeadId"].ToString();
            txtCustomerId.Text = _dtCallMeAgentLeads.Rows[0]["CustomerId"].ToString();
            txtCarId.Text = _dtCallMeAgentLeads.Rows[0]["CarId"].ToString();
            lblCallType.Text = _dtCallMeAgentLeads.Rows[0]["CallType"].ToString();

            outAgentLeadID = Convert.ToInt64(lblAgentLeadID.Text);
            outCustomerID = Convert.ToInt64(txtCustomerId.Text);
            outCarID = Convert.ToInt64(txtCarId.Text);
        }
    }

    public void LoadCallMeUser(Int64 _CallMeId)
    {
        DataSet _dsSummary = new DataSet();
        DataTable _dtSummary = new DataTable();
        string _CallMeDate = dateTimeNow.Day.ToString("00");
        Int32 _CallTime = 0;
        Int16 _Language = Convert.ToInt16(lblLangOption.Text);
        string _SearchKey = "email";

        MyDatabase _db = new MyDatabase("GG_CRMDBConnectionString");
        string _spName = "[sprocCXCarInsuranceOfflineCallMeSelectUserList]";
        SortedList _sPara = new SortedList();

        _sPara.Add("@CallMeDate", _CallMeDate);
        _sPara.Add("@CallTime", _CallTime);
        _sPara.Add("@Language", _Language);
        _sPara.Add("@SearchKey", _SearchKey);
        _sPara.Add("@CallMeId", _CallMeId);

        _dsSummary = _db.GetDataSet(_spName, _sPara);
        if (_dsSummary != null && _dsSummary.Tables.Count > 0 && _dsSummary.Tables[0].Rows.Count > 0)
        {
            txtMobileNoSearch.Text = _dsSummary.Tables[0].Rows[0]["ContactNo"].ToString();
        }
        else
        {
            txtMobileNoSearch.Text = string.Empty;
        }
    }

}
